package com.zerowidth.moc.web.utils;

import java.util.Calendar;

public class TimeParseUtil {

    /**
     * @param date 格式如 2022-12-01
     * @return
     */
    public static Calendar parseYYYYMMDD(String date) {
        try {
            Calendar calendar = Calendar.getInstance();
            String[] strings = date.split("-");

            calendar.set(Calendar.YEAR, Integer.parseInt(strings[0]));
            calendar.set(Calendar.MONTH, Integer.parseInt(strings[1]) -1);
            calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(strings[2]));

            return calendar;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * @param dateTime 格式如 "yyyy-MM-dd HH:mm:ss"
     * @return
     */
    public static Calendar parseYYYYMMDDHHmmss(String dateTime) {
        try {
            String date = dateTime.split(" ")[0];
            String time = dateTime.split(" ")[1];

            Calendar calendar = Calendar.getInstance();

            String[] dateArr = date.split("-");
            calendar.set(Calendar.YEAR, Integer.parseInt(dateArr[0]));
            calendar.set(Calendar.MONTH, Integer.parseInt(dateArr[1]) -1);
            calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dateArr[2]));

            String[] timeArr = time.split(":");
            calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(timeArr[0]));
            calendar.set(Calendar.MINUTE, Integer.parseInt(timeArr[1]));
            calendar.set(Calendar.SECOND, Integer.parseInt(timeArr[2]));

            return calendar;
        } catch (Exception e) {
            return null;
        }
    }

}
